#ifndef _CH_STRIUTILS_H__
#define _CH_STRIUTILS_H__

#pragma once

#define CHGetExeFilePath	CHStrUtils::GetExeFilePath
#define CHGetExeDirPath		CHStrUtils::GetExeDirPath
#define CHBuildPath			CHStrUtils::BuildPath
#define CHMessageBox		CHStrUtils::MessageBox
#define CHGetNameFromPath	CHStrUtils::GetNameFromPath
#define CHFilsIsExist		CHStrUtils::FileIsExist
#define CHReadFile			CHStrUtils::ReadFile
#define CHSplitString		CHStrUtils::SplitString

class CHStrUtils
{
public:
	static void GetExeFilePath(CString& strPath)
	{
		LPTSTR lpBuffer = strPath.GetBuffer(MAX_PATH);
		ULONG nNameLen = ::GetModuleFileName(NULL, lpBuffer, MAX_PATH);
		strPath.ReleaseBuffer(nNameLen);
	}

	static CString GetExeFilePath()
	{
		CString strRet;
		GetExeFilePath(strRet);
		return strRet;
	}

	static void GetExeDirPath(CString& strPath)
	{
		LPTSTR lpBuffer = strPath.GetBuffer(MAX_PATH);
		ULONG nNameLen = ::GetModuleFileName(NULL, lpBuffer, MAX_PATH);
		while(lpBuffer[nNameLen] != _T('\\') ) 
			--nNameLen;
		strPath.ReleaseBuffer(nNameLen+1);
	}
	static CString GetExeDirPath()
	{
		CString strRet;
		GetExeDirPath(strRet);
		return strRet;
	}

	static CString BuildPath(const CString& strFileName)
	{
		CString strRet;
		GetExeDirPath(strRet);
		return strRet + strFileName;
	}

	static void BuildPath(char *szBuffer, char *szFileName)
	{
		GetModuleFileName(NULL, szBuffer, MAX_PATH);
		int i = strlen(szBuffer)-1;
		while(szBuffer[i] != '\\') i--;
		szBuffer[i] = 0;
		strcat(szBuffer, "\\");
		strcat(szBuffer, szFileName);
	}

	static int MessageBox(LPCTSTR lpszFormat, ...)
	{
		ASSERT(AfxIsValidString(lpszFormat));

		CString str;

		va_list argList;
		va_start(argList, lpszFormat);
		str.FormatV(lpszFormat, argList);
		va_end(argList);

		return AfxMessageBox(str, MB_ICONINFORMATION | MB_OK);
	}

	static CString GetNameFromPath(const CString& strPath)
	{
		int nTemp=0, nStart=0;
		while(TRUE)
		{
			nTemp = strPath.Find("\\", nTemp);
			if(nTemp == -1)
				break;
			nStart = nTemp++;
		}
	
		if(nStart != -1)
			return strPath.Mid(nStart+1);
		else
			return strPath;
	}

	static BOOL FileIsExist(LPCTSTR pstrName)
	{
		CFileFind finder;
		return finder.FindFile(pstrName);
	}

	static BOOL ReadFile(const CString& strFile, CString& strBuffer)
	{
		CFile file;
		if(!file.Open(strFile, CFile::modeRead | CFile::shareDenyNone))
			return false;

		const ULONG nFileLen = file.GetLength();
		LPTSTR lpBuffer = strBuffer.GetBuffer(nFileLen);

		ULONG nReaded = 0, nTemp = 0;
		while(TRUE)
		{
			nTemp = file.Read(lpBuffer + nReaded, nFileLen - nReaded);
			nReaded += nTemp;
			if(nReaded >= nFileLen)	break;
		}

		ASSERT(nReaded == nFileLen);
		strBuffer.ReleaseBuffer(nFileLen);

		file.Close();
		return true;
	}

	static CString SplitString(const CString& strString, const CString& strSplit, int& nStart)
	{
		ASSERT(strSplit.GetLength() > 0);

		if(nStart < 0 || nStart > strString.GetLength())
		{
			nStart = -1;
			return _T("");
		}

		int nPos = strString.Find(strSplit, nStart);
		if(nPos == -1) nPos = strString.GetLength();
		ASSERT(nPos >= nStart);

		CString strResult = strString.Mid(nStart, nPos - nStart);
		nStart = nPos + strSplit.GetLength();
		return strResult;
	}
};

#endif //_CH_STRIUTILS_H__
